/**
    Die Ewigen Extension
    Copyright (C) 2011 DonKrieg @ sonkyu@googlemail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

(function($) {
    $.COLLECTORLIMIT = 400;

    /** sector class */
    $.ewigen.sector = function() {
    }

    $.fn.ewigen.sector = function() {
    }

    /**
     * disables the "green" rank, if the player has over 400 collectors
     * a player cannot attack aliens anymore when reaching a specific collector limit
     *
     * TODO: uhhhmmm, somehow the function should be in $.fn.ewigen.sector.fixDX61a23attacklimit
     */
    $.fn.fixDX61a23attacklimit = function() {
        // every element found
        this.each(
            function() {
                // reference for our current jquery object
                var obj     = $(this);
                // get the path
                var path    = obj.attr("src");

                // we need the players collectors
                chrome.extension.sendRequest(
                    {action: "retrieveData", key: "sde_collectors"}, function(response) {
                        if($.COLLECTORLIMIT >= parseInt(response.value)) {
                            // get the graphic
                            var temp    = path.slice(path.lastIndexOf("/"), path.length);

                            // the alien is attackable
                            if(indexOf("_g") >= 0) {
                                // replace the graphic
                                path        = path.substr(0, path.lastIndexOf("/"));
                                path        = path.concat(temp.replace("g", "r"));

                                // set the new path
                                obj.attr("src", path);
                            }
                        }
                    }
                );  // chrome.extension.sendRequest
            }
        );  // this.each
    }

    /** parse the sector data */
    $.ewigen.sector.parse = function() {
        //TODO: implement
    }

    /** get the current sector attack limits */
    $.ewigen.sector.attacklimits = function(data) {
        var MATCH = "Angriffsgrenze: ";

        var sector = 0;
        var fields = document.getElementsByTagName("input");

        for(var i=0; i < fields.length; i++) {
            if(fields[i].name == "sf") {
                sector = fields[i].value;
                break;
            }
        }

        // iterate through the "td"s of the page until we found the desired one
        for(var j=0; j < data.length; j++) {
            var start   = data[j].innerText.indexOf(MATCH);
            // found it
            if(start >= 0) {
                // extract the value
                var val = data[j].innerText.substring(start + MATCH.length, start + MATCH.length + 5);

                // save the data
                $.ewigen.util.CS2BG(
                    {
                        action:"storeData",
                        key: "sde_"+sector+"_attacklimits",
                        value: val
                    }
                );

                // save the data
                $.ewigen.util.CS2BG(
                    {
                        action:"storeData",
                        key: "sde_current_sector",
                        value: sector
                    }
                );

                // we don't need any more iterations
                break;
            }
        }
    }
})(jQuery)
